package org.hu.controller;

import org.hu.model.Department;
import org.hu.model.Employee;
import org.hu.model.Meeting;
import org.hu.model.MeetingDTO;
import org.hu.service.DepartmentService;
import org.hu.service.EmployeeService;
import org.hu.service.MeetingRoomService;
import org.hu.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 会议
 * @author Htrees
 */
@Controller
public class MeetingController {
    @Autowired
    MeetingRoomService meetingRoomService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    MeetingService meetingService;

    /**
     * 预定会议页面
     * @param model
     * @return bookmeeting
     */
    @RequestMapping("/bookmeeting")
    String bookMeeting(Model model){
        model.addAttribute("mrs",meetingRoomService.getMrByStatus(0));
        return "bookmeeting";
    }

    /**
     * 获取所有部门
     * @return JSON
     */
    @RequestMapping("/alldeps")
    @ResponseBody
    public List<Department> getAllDeps(){
        return departmentService.getAllDeps();
    }

    /**
     * 查询部门所属员工
     * @param depId 部门id
     * @return JSON
     */
    @RequestMapping("/getEmpByDep")
    @ResponseBody
    public List<Employee> getEmpByDepId(Integer depId){
        return employeeService.getEmpByDep(depId);
    }

    /**
     * 增加会议
     * @param meeting 会议对象
     * @param mps 参加员工数组
     * @param session
     * @return searchmeetings
     */
    @RequestMapping("/doAddMeeting")
    public String doAddMeeting(Meeting meeting, Integer[] mps, HttpSession session) {
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        meeting.setReservationistid(currentuser.getEmployeeid());
        Integer result = meetingService.addMeeting(meeting,mps);
        if (result == 1) {
            return "redirect:/searchmeetings";
        }else{
            return "bookmeeting";
        }
    }

    //每页大小
    public  static final Integer PAGE_SIZE = 10;

    /**
     * 查询会议并分页
     * @param meetingDTO 会议
     * @param page 页数
     * @param model
     * @return
     */
    @RequestMapping("/searchmeetings")
    public String getAllMeetings(MeetingDTO meetingDTO, @RequestParam(defaultValue = "1") Integer page, Model model) {
        List<MeetingDTO> listMeetingDTOs = meetingService.listMeetingDTOs(meetingDTO, page, PAGE_SIZE);

        Long total = meetingService.getTotal(meetingDTO);

        model.addAttribute("meetings", listMeetingDTOs);
        model.addAttribute("total", total);
        model.addAttribute("page", page);
        model.addAttribute("pagenum", total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);

        return "searchmeetings";
    }

    /**
     * 通过会议id查询会议详情
     * @param meetingid 会议id
     * @param model
     * @return meetingdetails
     */
    @RequestMapping("meetingdetails")
    public String meetingdetails(Integer meetingid,Model model){
        Meeting meeting = meetingService.getMtById(meetingid);
        model.addAttribute("mt",meeting);
        List<Employee> listEmployee = employeeService.getEmpsByid(meetingid);
        model.addAttribute("emps",listEmployee);
        return "meetingdetails";
    }

    /**
     * 当前用户参加的会议
     * @param session
     * @param model
     * @return mymeetings
     */
    @RequestMapping("/mymeetings")
    public String mymeetings(HttpSession session,Model model){
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        List<MeetingDTO> mts = meetingService.getMtByEmpId(currentuser.getEmployeeid());
        model.addAttribute("mts",mts);
        return "mymeetings";
    }

}
